import socket
import pymysql
from setting import *

"""
接受客户端的请求，对请求进行校对，并给客户端反馈
解释请求格式，请求结果，并分发给对应的接口进行处理
"""


class RequestParsing:
    def __init__(self, msg):
        self.request_type, \
        request_content, \
        self.http_protocol = msg.split(' ')
        self.api, self.params = request_content.split('?')
        self.sapi = self.api.strip('/').split('/')[-1]

    def get_params(self):
        req_params = {}
        for para in self.params.split('&'):
            k, v = para.split('=')
            req_params[k] = v
        return req_params


class RequestHandle:
    def __init__(self, msg, conn, recv_size=1024):
        # self.request_msg = msg
        self.conn = conn
        self.recv_size = recv_size
        self.requestparsing = RequestParsing(msg)

    def main_handle(self):
        print(self.requestparsing.api)

        if self.requestparsing.request_type == 'GET':
            print('get 请求')
            # 处理登录
            if self.requestparsing.api.startswith('/login'):
                req_params = self.requestparsing.get_params()

                # 交给api_login处理
                from api_login import LoginHandle

                l = LoginHandle(**req_params, conn=self.conn)
                l.student_login_handle()

            elif self.requestparsing.api.startswith('/tlogin'):
                req_params = self.requestparsing.get_params()

                # 交给api_login处理
                from api_login import LoginHandle

                l = LoginHandle(**req_params, conn=self.conn)
                l.teacher_login_handle()

            # 处理注册
            elif self.requestparsing.api.startswith('/regist'):
                son_api = self.requestparsing.sapi

                from api_regist import RegistHandle

                if son_api == 'student':
                    req_params = self.requestparsing.get_params()
                    r = RegistHandle(**req_params, conn=self.conn)
                    r.regist_handle()

                elif son_api == 'teacher':
                    req_params = self.requestparsing.get_params()
                    r = RegistHandle(**req_params, conn=self.conn)
                    r.teacher_regist_handle()

            # 处理约课
            elif self.requestparsing.api.startswith('/orderlist'):
                son_api = self.requestparsing.sapi

                from api_order import OrderList
                ol = OrderList(self.conn)

                if son_api == 'order':
                    ol.order_list_get()

                elif son_api == 'ordercancel':
                    ol.order_cancel()

                elif son_api == 'classcancel':
                    rp = self.requestparsing.get_params()
                    ol.class_cancel(**rp)

                elif son_api == 'info':
                    ol.order_list_info()

                # 教师接口
                elif son_api == 'tinfo':
                    rp = self.requestparsing.get_params()
                    ol.teacher_class_info(**rp)

                # 教师接口
                elif son_api == 'getordercount':
                    rp = self.requestparsing.get_params()
                    ol.class_order_count(**rp)

                elif son_api == 'add':
                    rp = self.requestparsing.get_params()
                    ol.order_list_add(**rp)

                # 教师接口
                elif son_api == 'class_add':
                    rp = self.requestparsing.get_params()
                    ol.class_add(**rp)

                elif son_api == 'sorderinfo':
                    rp = self.requestparsing.get_params()
                    ol.order_list_get(**rp)

            elif self.requestparsing.api.startswith('/videoinfo'):
                    pass

            elif self.requestparsing.api.startswith('/comment'):
                from api_comment import CommentHandle

                if self.requestparsing.sapi == 'teacher':
                    rp = self.requestparsing.get_params()

                    teacher_comment = CommentHandle(conn=self.conn)
                    teacher_comment.teacher_get_comment(**rp)

                elif self.requestparsing.sapi == 'student':
                    rp = self.requestparsing.get_params()

                    student_comment = CommentHandle()
                    student_comment.get_student_comment(**rp)

                elif self.requestparsing.sapi == 'reply':
                    rp = self.requestparsing.get_params()

                    teacher_reply = CommentHandle(self.conn)
                    teacher_reply.teacher_reply_comment(**rp)

        elif self.requestparsing.request_type == 'POST':
            print('post请求')


if __name__ == '__main__':
    test = RequestHandle(msg='GET /comment/teacher?teacher_id=2 socket', conn=None)
    test.main_handle()
